#include "DialogPlugin.h"
#include "EPlugin.h"
#include "ui_DialogPlugin.h"
#include <EPluginLoader.h>

DialogPlugin::DialogPlugin(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogPlugin)
{
    ui->setupUi(this);
    QList<int> splitterSize;
    splitterSize<<1<<4;
    // ui->splitter->setStretchFactor(0, 1);
    // ui->splitter->setStretchFactor(0, 0);
    ui->splitter->setSizes(splitterSize);

    QObject::connect(ui->buttonOk, 
                     SIGNAL(clicked(bool)),
                     this,
                     SLOT(slotButtonOk(bool ))
                     );
    QObject::connect(ui->buttonCancel, 
                     SIGNAL(clicked(bool)),
                     this,
                     SLOT(slotButtonCancel(bool ))
                     );
    QObject::connect(ui->listWidget, 
                     SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
                     this,
                     SLOT(slotListCurrentItemChanged(QListWidgetItem*, QListWidgetItem*))
                     );

    // WidgetVariantMatrix* wPtr = new WidgetVariantMatrix;
    // QListWidgetItem* item = new QListWidgetItem();
    // ui->listWidget->addItem(item);
    // ui->listWidget->setItemWidget(item, wPtr);
}

DialogPlugin::~DialogPlugin()
{
    delete ui;
}

void DialogPlugin::updateFromPluginLoader()
{
    ui->listWidget->clear();
    for(size_t i = 0; i < EPluginLoader::instance()->getPluginSize(); i++)
    {
        EPluginPtr p = EPluginLoader::instance()->getPlugin(i);
        ui->listWidget->addItem(p->getName().data());
    }
}

void DialogPlugin::slotButtonOk(bool value)
{
    //QListWidgetItem* item = ui->listWidget->currentItem();
    //if(item != nullptr)
    //{
    //    m_selectedTypeName = item->text();
    //}
    
    this->accept();
}

void DialogPlugin::slotButtonCancel(bool value)
{
    //m_selectedTypeName = "";
    this->accept();
}

void DialogPlugin::slotListCurrentItemChanged(QListWidgetItem* current, QListWidgetItem* previous)
{
    int currentRow = ui->listWidget->currentRow();
    EPluginPtr p = EPluginLoader::instance()->getPlugin(currentRow);
    if(p != nullptr)
    {
        QString text(p->getDescription().data());
        ui->textEdit->setText(text);
    }
}
